package com.example.demo.myArray;

import java.util.Arrays;

/**
 * 自己实现的数组
 */
public class MyList {

    private Object[] objects;

    private int size = 0;


    public MyList(int capacity) {
        this.objects = new Object[capacity];
    }


    public MyList() {
        this.objects = new Object[5];
    }


    public void add(Object o){
        if (size == objects.length){
            // 说明容量满了，得扩容
            Object[] objects1 = new Object[size+5];
            System.arraycopy(objects,0,objects1,0,size);
            objects = objects1;
        }
        objects[size++] = o;
    }

    public Object get(int index){
        return objects[index];
    }

    public int getSize(){
        return size;
    }

    public Object[] merge(Object[] o1,Object[]o2){
        int len1 = o1.length;
        int len2 = o2.length;
        Object[] result = new Object[len1+len2];
        if (len1>=len2){
            for (int i =0;i<len1;i++){
                result[i] = o1[i];
                if (i<len2){
                    result[i+len1] = o2[i];
                }
            }
        }else {
            for (int i =0;i<len2;i++){
                result[i] = o2[i];
                if(i<len1){
                    result[i+len2] = o1[i];
                }
            }
        }
        Arrays.sort(result);
        return result;
    }

}



class TestMyList{

    public static void main(String[] args) {
        MyList myList = new MyList();

//        for (int i =0;i<5;i++){
//            myList.add(i);
//        }
//        int size = myList.getSize();
//        for (int i =0;i<size;i++){
//            System.out.println(myList.get(i));
//        }
        Integer[] i1 = {1,2,3,4,5,6};
        Integer[] i2 = {7,8,9};
        Object[] merge = myList.merge(i1, i2);
        for (Object o:merge) {
            System.out.println(o);
        }
    }
}